//package com.yonghui.springsecurityoauth2demo.filter;
//
//
//
//
//import com.alibaba.fastjson2.JSON;
//import com.yonghui.springsecurityoauth2demo.pojo.AjaxResult;
//import com.yonghui.springsecurityoauth2demo.pojo.LoginBody;
//import com.yonghui.springsecurityoauth2demo.pojo.LoginUser;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.AuthenticationServiceException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Component
//public class LoginFilter extends UsernamePasswordAuthenticationFilter {
//    private static final Logger LOGGER = LoggerFactory.getLogger(LoginFilter.class);
//
////    private final ISysSecretService sysSecretService;
////
////    private final ISysConfigService configService;
////
////    private final RedisCache redisCache;
//
//    private final UserDetailsService userDetailsService;
//
//    @Lazy // 延迟加载，不然启动时会形成循环依赖
//    public LoginFilter(AuthenticationManager authenticationManager,
//                        UserDetailsService userDetailsService) {
//        super(authenticationManager);
////        this.sysSecretService = sysSecretService;
////        this.configService = configService;
////        this.redisCache = redisCache;
//        this.userDetailsService = userDetailsService;
//        super.setAuthenticationSuccessHandler((HttpServletRequest req, HttpServletResponse resp, Authentication auth) -> {
//            LoginUser loginUser = (LoginUser) auth.getPrincipal();
//            // 清除密码
////            loginUser.getUser().setPassword(null);
//            // 记录登录信息
//            //userService.recordLoginInfo(loginUser.getUserId());
//            AjaxResult.success(resp);
//        });
//        super.setAuthenticationFailureHandler((HttpServletRequest req, HttpServletResponse resp, AuthenticationException ex) -> {
//            if (ex instanceof LoginFailureException) {
//                AjaxResult.error(resp, ex.getMessage());
//            } else {
//                AjaxResult.error(resp, "用户名或密码错误");
//            }
//        });
//    }
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        if (!"POST".equals(request.getMethod())) {
//            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
//        }
//        LoginBody loginBody;
//        try {
//            //JSONUtil.t
//            //JSON.
//            //loginBody = JSON.parseObject(request.getInputStream(), LoginBody.class);
//            loginBody = JSON.parseObject(request.getInputStream(), LoginBody.class);
//        } catch (IOException e) {
//            LOGGER.info("请求参数异常", e);
//            throw new LoginFailureException("请求参数异常");
//        }
//
//        // 验证码
//        String username = loginBody.getUsername();
////        if (configService.selectCaptchaOnOff()) {
////            validateCaptcha(username, loginBody.getCode(), loginBody.getUuid());
////        }
//        String pwd = sysSecretService.decodePassword(loginBody.getPassword());
//        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, pwd);
//        setDetails(request, authRequest);
//        return this.getAuthenticationManager().authenticate(authRequest);
//
//    }
//
//    /**
//     * 校验验证码
//     *
//     * @param username 用户名
//     * @param code     验证码
//     * @param uuid     唯一标识
//     */
////    private void validateCaptcha(String username, String code, String uuid) {
////        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + CarcheckStringUtils.nvl(uuid, "");
////        String captcha = redisCache.getCacheObject(verifyKey);
////        redisCache.deleteObject(verifyKey);
////        if (captcha == null) {
////            String message = MessageUtils.message("user.jcaptcha.expire");
////            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, message));
////            throw new LoginFailureException(message);
////        }
////        if (!code.equalsIgnoreCase(captcha)) {
////            String message = MessageUtils.message("user.jcaptcha.error");
////            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, message));
////            throw new LoginFailureException(message);
////        }
////    }
//}
